<?php

namespace app\controller;

use Overtrue\Pinyin\Pinyin;


class Project extends Base
{

    public function initialize()
    {
        parent::initialize();
        // Token::apisecurity();
    }

    public function index()
    {
        if (isset(_get()['id']) && _get()['id']) {
            $where = [];
            $where[] = ['delete_time', '=', 0];
            $where[] = ['id', '=', _get()['id']];
            $result = db('project', 'project')->where($where)->find();
            $result['create_time'] = totime($result['create_time']);
            $result['update_time'] = totime($result['update_time']);
            _Success('项目信息', 200, $result);
        } else {
            $where = [];
            $where[] = ['delete_time', '=', 0];
            $list = db('project', 'project')->where($where)->select()->each(function ($item) {
                $item['create_time'] = totime($item['create_time']);
                $item['update_time'] = totime($item['update_time']);
                return $item;
            })->toArray();
            _Success('项目列表', 200, $list);
        }
    }

    public function project_add()
    {
        if (_ispost()) {
            if (!isset(_post()['project_name']) || !_post()['project_name']) {
                _Error('项目名称未填写');
            }
            $data = _post();
            $data['create_time'] = time();
            $data['update_time'] = time();
            $result = db('project', 'project')->insert($data);
            if ($result) {
                return _Success('添加成功');
            }
            return _Error('添加失败');
        }
    }


    public function project_edit()
    {
        if (_ispost()) {
            if (!isset(_post()['id']) || !_post()['id']) {
                _Error('项目主键未提交');
            }
            if (!isset(_post()['project_name']) || !_post()['project_name']) {
                _Error('项目名称未填写');
            }
            $data = _post();
            $data['update_time'] = time();
            $result = db('project', 'project')->where('id', $data['id'])->update($data);
            if ($result) {
                return _Success('更新成功');
            }
            return _Error('更新失败');
        }
    }
}
